package com.humble.humbleojjudgeservice.judge.strategy.impl;/*
  humble keep gonging
*/


import com.humble.humbleojjudgeservice.judge.strategy.JudgeStrategy;
import com.humble.humbleojmodel.model.dto.Question.JudgeConfig;
import com.humble.humbleojmodel.model.dto.QuestionSubmit.JudgeInfo;
import com.humble.humbleojmodel.model.enums.JudgeInfoMessageEnum;
import com.humble.humbleojmodel.model.enums.QuestionSubmitStatusEnum;
import com.humble.humbleojmodel.model.judge.JudgeContext;
import com.humble.humbleojmodel.model.judge.JudgeResult;

import java.util.List;

public class PythonJudgeStrategy implements JudgeStrategy { ;

    @Override
    public JudgeResult doJudge(JudgeContext judgeContext) {
        //获取修改行
        JudgeConfig judgeConfig = judgeContext.getJudgeConfig();
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();

        //沙箱返回状态为错误
        if(judgeContext.getResponseStatus()==1){
            judgeInfo.setMessage(JudgeInfoMessageEnum.running_error.getValue());
            return new JudgeResult(QuestionSubmitStatusEnum.FAILURE.getValue(),judgeInfo);
        }

        //超时或超出内存限制,python语法放宽限制
        if(5*judgeConfig.getTimeLimit()<judgeInfo.getTimeConsume()){
            judgeInfo.setMessage(JudgeInfoMessageEnum.time_limit_exceeded.getValue());
            return new JudgeResult(QuestionSubmitStatusEnum.FAILURE.getValue(),judgeInfo);
        }
        if(5*judgeConfig.getMemoryLimit()<judgeInfo.getMemoryConsume()){
            judgeInfo.setMessage(JudgeInfoMessageEnum.memory_limit_exceeded.getValue());
            return new JudgeResult(QuestionSubmitStatusEnum.FAILURE.getValue(),judgeInfo);
        }

        List<String> standardOutputList = judgeContext.getStandardOutputList();
        List<String> outputList = judgeContext.getOutputList();
        if(standardOutputList.size()!=outputList.size()){
            //输出数量不同直接设置为错
            judgeInfo.setMessage(JudgeInfoMessageEnum.WRONG_ANSWER.getValue());
            return new JudgeResult(QuestionSubmitStatusEnum.FAILURE.getValue(),judgeInfo);
        }

        for(int i=0;i<standardOutputList.size();i++){
            if(!standardOutputList.get(i).equals(outputList.get(i))){
                judgeInfo.setMessage(JudgeInfoMessageEnum.WRONG_ANSWER.getValue());
                return new JudgeResult(QuestionSubmitStatusEnum.FAILURE.getValue(),judgeInfo);
            }
        }
        //没有错误设置为正确；
        judgeInfo.setMessage(JudgeInfoMessageEnum.ACCEPTED.getValue());
        return new JudgeResult(QuestionSubmitStatusEnum.SUCCESS.getValue(),judgeInfo);

    }
}
